package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Product;
import cn.wolfcode.crm.domain.ProductStock;
import cn.wolfcode.crm.domain.Productstockallocation;
import cn.wolfcode.crm.domain.Shop;
import cn.wolfcode.crm.mapper.ProductStockMapper;
import cn.wolfcode.crm.mapper.ProductstockallocationMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;

@Service
public class ProductStockServiceImpl implements cn.wolfcode.crm.service.IProductStockService {

    @Autowired
    private ProductStockMapper productStockMapper;
    @Autowired
    private ProductstockallocationMapper poaMapper;


    public PageResult queryForList(QueryObject qo) {

        List<ProductStock> list = productStockMapper.queryForList(qo);

        return new PageResult(list.size(),list);
    }


    //盘点确认数量
    public void auditNumber(Long id) {
        //1.获取相应的chanpin
        ProductStock stock = productStockMapper.selectByPrimaryKey(id);
        stock.setLastCheckTime(new Date());

        productStockMapper.updateByAuditNumber(stock);
    }

    //修改库存数量
    public void updateReviseNumber(ProductStock stock) {
        productStockMapper.updateReviseNumber(stock);
    }

    public void allocation(ProductStock stock) {

        //1.获取原先的stock
        ProductStock oldStock = productStockMapper.selectByPrimaryKey(stock.getId());
        if(stock.getDepot().getId() ==oldStock.getDepot().getId()){
            throw  new RuntimeException("同一个仓库不需要调拨");
        }else{
            System.out.println(stock.getNumber().compareTo(oldStock.getNumber()));
            if(stock.getOutNumber().longValue()>oldStock.getNumber().longValue()){
                throw new RuntimeException("调拨的数量超过库存数量,请重新操作");
            }
            BigDecimal orginNumber = oldStock.getNumber();
            //设置stock的属性
            //1.设置原先stock的属性
            System.out.println(oldStock.getNumber().subtract(stock.getOutNumber()));
            oldStock.setNumber(oldStock.getNumber().subtract(stock.getOutNumber()));
            System.out.println(oldStock.getNumber());
            oldStock.setAmountPrice(oldStock.getNumber().multiply(oldStock.getCostPrice()).setScale(2, RoundingMode.HALF_UP));
            //2.更新圆库存
            productStockMapper.updateNumberAndAmount(oldStock);
            //3.设置调拨的库存。
            //1.商品
            stock.setProduct(oldStock.getProduct());
            stock.setNumber(stock.getOutNumber());
            //2.入库时间
            stock.setInputTime(new Date());
            //3.小计
            stock.setAmountPrice(stock.getNumber().multiply(oldStock.getCostPrice()).setScale(2,RoundingMode.HALF_UP));
            //4.单据
            stock.setCostPrice(oldStock.getCostPrice());
            //5.实收
            stock.setReceipts(oldStock.getReceipts());
            //sn
            stock.setSn(oldStock.getSn());

            //插入新库存
            productStockMapper.insert(stock);

            //插入库存调拨单
            Productstockallocation poa = new Productstockallocation();
            poa.setIntoShop(stock.getDepot());
            poa.setOutShop(oldStock.getDepot());
            poa.setProduct(oldStock.getProduct());
            poa.setOutNumber(stock.getOutNumber());
            poa.setOutShopOriginNumber(orginNumber);
            poa.setOutShopNowNumber(oldStock.getNumber());
            poa.setIntoShopNowNumber(stock.getNumber());

            //插入调拨单
            poaMapper.insert(poa);

        }

    }

    public PageResult queryForResult(QueryObject qo) {
        List<ProductStock> list = productStockMapper.queryForResult(qo);

        return new PageResult(list.size(),list);

    }


}
